<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{ $t("nousi_system") }}</h3>

      <el-tabs v-model="activeLogin" stretch>
        <el-tab-pane :label="$t('password_login')" name="1">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" :placeholder="$t('account_number')">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" :placeholder="$t('password')"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input v-model="loginForm.code" auto-complete="off" :placeholder="$t('code')" style="width: 63%"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane :label="$t('phone_login')" name="2">
          <el-form-item prop="phone">
            <el-input v-model="loginForm.phone" type="text" auto-complete="off" :placeholder="$t('mobile_phone_number')">
              <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
              <el-select class="select-mobile_area_code" v-model="loginForm.phoneAreaCode" slot="prepend"
                :placeholder="$t('please_select', { name: $t('region') })" style="width: 120px">
                <el-option v-for="dict in dict.type.mobile_area_code" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-input>
          </el-form-item>
          <el-form-item prop="phoneCode">
            <div style="display: flex;">
              <el-input v-model="loginForm.phoneCode" auto-complete="off" :placeholder="$t('code')"
                @keyup.enter.native="handleLogin" style="width: 220px">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <el-button style="margin-left: 10px" :disabled="disabledPhone" @click="getPhoneCode">{{ valPhoneBtn
              }}</el-button>
            </div>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane :label="$t('email_login')" name="3">
          <el-form-item prop="email">
            <el-input v-model="loginForm.email" type="text" auto-complete="off" :placeholder="$t('emailbox')">
              <svg-icon slot="prefix" icon-class="email" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="emailCode">
            <div style="display: flex;">
              <el-input v-model="loginForm.emailCode" auto-complete="off" :placeholder="$t('code')"
                @keyup.enter.native="handleLogin" style="width: 220px">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <el-button style="margin-left: 10px" :disabled="disabledEmail" @click="getEmailCode">{{ valEmailBtn
              }}</el-button>
            </div>
          </el-form-item>
        </el-tab-pane>
        <div style="display: flex;justify-content: space-between;align-items: center;">
          <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">{{ $t("remember_password")
        }}</el-checkbox>
        <a style="margin: 0px 0px 25px 0px">忘记密码</a>
        </div>
        
        <el-form-item style="width: 100%">
          <el-button :loading="loading" size="medium" type="primary" style="width: 100%"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">{{ $t("login") }}</span>
            <span v-else>loading...</span>
          </el-button>
          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'">{{
              $t("register_now")
            }}</router-link>
          </div>
        </el-form-item>
      </el-tabs>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <!-- <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg, getValidateCode } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  name: "Login",
  dicts: ["mobile_area_code"],
  components: {},
  data() {
    return {
      activeLogin: "1",
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        phone: null,
        phoneAreaCode: "86",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("please_enter", {
              name: this.$t("account_number"),
            }),
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("please_enter", { name: this.$t("password") }),
          },
        ],
        code: [
          {
            required: true,
            trigger: "change",
            message: this.$t("please_enter", { name: this.$t("code") }),
          },
        ],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,

      valPhoneBtn: this.$t("get_verification_code"),
      disabledPhone: false,
      valEmailBtn: this.$t("get_verification_code"),
      disabledEmail: false,

      lookViewUrl: "http://192.168.0.29:8081/111.xlsx",
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();

  },
  methods: {

    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        phoneAreaCode: "86",
      };
    },
    handleLogin() {
      if (this.activeLogin === "1") {
        if (!this.loginForm.username) {
          this.$message.warning(
            this.$t("please_enter", { name: this.$t("account_number") })
          );
          return;
        }
        if (!this.loginForm.password) {
          this.$message.warning(
            this.$t("please_select", { name: this.$t("password") })
          );
          return;
        }
      }
      if (this.activeLogin === "2") {
        if (!this.loginForm.phone) {
          this.$message.warning(
            this.$t("please_enter", { name: this.$t("mobile_phone_number") })
          );
          return;
        }
        if (!this.loginForm.phoneCode) {
          this.$message.warning(
            this.$t("please_select", { name: this.$t("code") })
          );
          return;
        }
      }
      if (this.activeLogin === "3") {
        if (!this.loginForm.email) {
          this.$message.warning(
            this.$t("please_enter", { name: this.$t("emailbox") })
          );
          return;
        }
        if (!this.loginForm.emailCode) {
          this.$message.warning(
            this.$t("please_select", { name: this.$t("code") })
          );
          return;
        }
      }
      this.loading = true;
      if (this.loginForm.rememberMe) {
        Cookies.set("username", this.loginForm.username, { expires: 30 });
        Cookies.set("password", encrypt(this.loginForm.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", this.loginForm.rememberMe, {
          expires: 30,
        });
      } else {
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      this.loginForm.activeLogin = this.activeLogin;
      this.$store
        .dispatch("Login", this.loginForm)
        .then(() => {
          this.$router.push({ path: this.redirect || "/" }).catch(() => { });
        })
        .catch(() => {
          this.loading = false;
          if (this.captchaEnabled) {
            this.getCode();
          }
        });
    },

    getPhoneCode() {
      if (!this.loginForm.phone) {
        this.$message.warning(
          this.$t("please_enter", { name: this.$t("mobile_phone_number") })
        );
        return;
      }
      if (!this.loginForm.phoneAreaCode) {
        this.$message.warning(
          this.$t("please_select", { name: this.$t("region") })
        );
        return;
      }
      getValidateCode({
        loginType: 2,
        areaCode: this.loginForm.phoneAreaCode,
        username: this.loginForm.phone,
      });
      let time = 60;
      let timer = setInterval(() => {
        if (time == 0) {
          clearInterval(timer);
          this.valPhoneBtn = this.$t("get_verification_code");
          this.disabledPhone = false;
        } else {
          this.disabledPhone = true;
          this.valPhoneBtn = this.$t("again_seconds", { name: time });
          time--;
        }
      }, 1000);
    },
    getEmailCode() {
      if (!this.loginForm.email) {
        this.$message.warning(
          this.$t("please_enter", { name: this.$t("emailbox") })
        );
        return;
      }
      getValidateCode({
        loginType: 1,
        username: this.loginForm.email,
      });
      let time = 60;
      let timer = setInterval(() => {
        if (time == 0) {
          clearInterval(timer);
          this.valEmailBtn = this.$t("get_verification_code");
          this.disabledEmail = false;
        } else {
          this.disabledEmail = true;
          this.valEmailBtn = this.$t("again_seconds", { name: time });
          time--;
        }
      }, 1000);
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  /* background-image: url("../assets/images/login-background.jpg"); */
  background: #f5f5f5;
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.select-mobile_area_code {
  .el-input__inner {
    padding: 0 5px;
  }
}
</style>
